﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponentsDx.Options;
using ProgNetComponentsDx.Language;
using ProgNetComponentsDx.Forms;
using ProgNetComponents.Data.SqlCe;
using ProgNetComponents.Ini;
using System.Data.SqlServerCe;
using System.IO;
using ProgNetComponentsDx.Layout;
using ProgNetComponentsDx.Browsers;
using ProgNetComponentsDx.Data;

namespace SqlCeManager.Init
{
    public partial class InitForm : BaseInitForm
    {
        public InitForm()
        {
            InitializeComponent();
            lblTitle.Text = Application.ProductName;
            this.Text = Application.ProductName;
            lblText.Text = "";
        }

        SqlCeDataManager helper;
        protected override ProgNetComponentsDx.Layout.LayoutGrabber CreateLayoutGrabber()
        {
            SqlCeLayoutGrabber g = new SqlCeLayoutGrabber();
            g.TableName = "STP_LAYOUT";
            g.XmlColumn = "STP_LAYOUT_XML_IMG";
            g.LayoutNameColumn = "STP_LAYOUT_NAZWA";
            g.ControlNameColumn = "STP_LAYOUT_KONTROLKA";
            return g;
        }

        protected override OptionGrabber CreateOptionGrabber()
        {
            SqlCeOptionGrabber g = new SqlCeOptionGrabber();
            g.OptionNameColumn = "STP_OP_NAZWA";
            g.OptionValueColumn = "STP_OP_WARTOSC";
            g.TableName = "STP_OPCJE";
            return g;
        }

        protected override void PreInit()
        {
            base.PreInit();
            IniFile iniFile = new IniFile(Application.StartupPath + "\\SqlCeManager.ini");
            iniFile.ReadIni();
            string dbFile = iniFile.GetValueFormCache("Main", "DB");
            if (dbFile.IsNullOrEmpty())
                dbFile = Application.StartupPath + "\\SqlCeManager.sdf";
            ProgNetComponents.Data.SqlCe.SqlCeDataManager.GlobalConnectionString = @"Data Source=" + dbFile + "; Encrypt Database=False; Max Database Size = 4091"; //File Mode=Read Write;
            helper = new SqlCeDataManager();

            if (!File.Exists(dbFile))
            {
                SqlCeEngine eng = new SqlCeEngine(SqlCeDataManager.GlobalConnectionString);
                eng.CreateDatabase();
                ExecuteQieries(Properties.Resources.CreateDb, "CreateDB");
            }
        }
        private void ExecuteQieries(string queries, string name)
        {
            string[] str = queries.XSplit(@"\s+GO;\s+", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
            for (int i=0; i < str.Length; i++)
            {
                try
                {
                    if (str[i].Trim().IsNotNull())
                    {
                        helper.ExecuteNonQuery(str[i]);
                        if (helper.LastException != null)
                            LogException(name, str[i], i, helper.LastException);
                    }
                }
                catch (Exception ex)
                {
                    LogException(name, str[i], i, ex);
                }
            }
        }
        private void LogException(string name, string query, int no, Exception ex)
        {
            string str = "-------------------------------------------" + Constants.NewLineLinux +
             ("Błąd w skrypcie: " + name + ", polecenie numer: " + no) + Constants.NewLineLinux +
             ("Skrypt: " + query) + Constants.NewLineLinux;
            ex.LogError(str);
        }

        protected override void InitLangouage()
        {
            base.InitLangouage();

            ProgNetLanguage pl = new ProgNetLanguage(new System.Globalization.CultureInfo("pl-PL"));
            pl.RegisterManager("default", typeof(Lng.default_pl_PL));
            pl.RegisterManager("menu", typeof(Lng.menu_pl_PL));

            ProgNetLanguage.SetCurrentLanguage(pl.Code);
            ProgNetLanguage.LogMissingValues = true;
        }
        protected override ProgNetComponents.Data.DataManager CreateDataManagerForBrowsers()
        {
            return new SqlCeDataManager();
        }
        protected override SelectDataManagerCreator CreateDataManagerCreator()
        {
            return new SqlCeSelectDataManagerCreator();
        }
        protected override DataTable BrowserCache_GetStaticData(object browserId)
        {
            DataTable dt = null;
            if (BrowserCache.IsTheSameBrowser(Init.InitFormBrowserNames.Glowna.TypyKolumn, browserId))
            {
                dt = new DataTable(Init.InitFormBrowserNames.Glowna.TypyKolumn.ToString());
                dt.Columns.Add("TYP");
                string[] types = new string[]{"bigint",
                        "int",
                        "smallint",
                        "tinyint",
                        "bit",
                        "numeric",
                        "money",
                        "float",
                        "real",
                        "datetime",
                        "nchar",
                        "nvarchar",
                        "ntext",
                        "binary",
                        "varbinary",
                        "image"};
                foreach (string t in types)
                    dt.Rows.Add(t);
            }
            return dt;
        }
    }
}
